<?php

namespace App\Controller\AppCommon;

use App\Base\BaseCache;
use App\Base\BaseController4Restful;
use App\Base\Format;
use App\Entity\Shop;
use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;

/**
 * @route("/app")
 */
class AppUserController extends BaseController4Restful
{

    /**
     * @Route("/send_sms",name="AppSendSms")
     */
    public function AppSendSms()
    {
        if (!$phone = $this->getParamRequired("phone")) {return $this->json_out(400,"请输入手机号");}
        $is = new Format();
        if (!$is->isPhone($phone)) {
            return $this->json_out(400,"手机号错误");
        }

        $lastTime = $this->getUserCache("smsLastTime");
        if ($lastTime > time() - 18) {
            return $this->json_out(400,"操作太频繁");
        }
        $smsRecentCount = $this->getUserCache("smsRecentCount");
        $smsRecentDate = $this->getUserCache("smsRecentDate");
        if (!$smsRecentCount || $smsRecentDate != date("Ymd") ) {
            $smsRecentCount = 1;
        }else{
            $smsRecentCount++;
        }
        if ($smsRecentCount > 50) {
            return $this->json_out(400,"尝试次数超出上限");
        }

        require_once __DIR__."/../../../BaseExt/Sms.php";
        $sms = new \Sms();
        $rand = rand(1000,9999);
//        $sms->sendSms($phone,"SMS_174166578",array("code"=>$rand));
        $this->setUserCache("smsCode",$rand);
        $this->setUserCache("smsLastTime",time());
        $this->setUserCache("smsLastPhone",$phone);
        $this->setUserCache("smsRecentCount",$smsRecentCount);
        $this->setUserCache("smsRecentDate",date("Ymd"));
        return $this->json_out(200,"已发送 $rand");
    }




    /**
     * @Route("/login_sms",name="AppUserSMSLogin")
     */
    public function AppUserSMSLogin(Request $request , UserRepository $userRepository)
    {
        if (!$phone = $this->getParamRequired("phone")) {return $this->json_out(400,"请输入手机号");}
        $is = new Format();
        if (!$is->isPhone($phone)) {return $this->json_out(400,"手机号错误");}
        $smsLastPhone = $this->getUserCache("smsLastPhone");
        if ($smsLastPhone != $phone) {return $this->json_out(400,"手机号不一致");}
        $smsCode = $this->getUserCache("smsCode");
        if (!$code = $this->getParamRequired("code")) {return $this->json_out(400,"请输入验证码");}
        if ($code != $smsCode) {return $this->json_out(400,"验证码错误");}
        $user = $userRepository->findOneBy(array("phone"=>$phone));
        if (!$user) {
            $user = new User();
            $user->setApiToken($phone)
                ->setPhone($phone)->setAvatar("/ApiCloud/assets/img/userHead.jpeg")->setPassword(md5($phone))
                ->setCreateAt(new \DateTime())
                ->setName("用户_".$phone);
        }
        $this->myEm()->persist($user);
        $this->myEm()->flush();
        //设置缓存
        $sessionData = array(
            "id" => $user->getId()
            ,"name"=>$user->getName()
            ,"phone"=>$user->getPhone()
            ,"avatar"=>$user->getAvatar()
        );
        //启用Cache
        $this->setUserCache('user',$sessionData);

        return $this->json_out(200,"登录成功",$sessionData);
    }





}
